home *** CD-ROM | disk | FTP | other *** search
- Path: nioz.nl!news
- From: rikko@nioz.nl (Rikko Verrijzer)
- Newsgroups: comp.lang.c
- Subject: freeing structures
- Date: 6 Feb 1996 10:03:15 GMT
- Organization: Netherlands Institute for Sea Research
- Message-ID: <4f7913$brg@sepia.nioz.nl>
- Reply-To: rikko@nioz.nl
- NNTP-Posting-Host: sepia.nioz.nl
-
- Hi,
-
- I have learnt C++ at my school past year, but now I have to build a programm in just
- plain Standard C. the problem with this is, that several C types where new to me.
- as example typedef.... in C++ I used a class to do this...
-
- /*
- * Data structure used for storing the loaded file.
- */
-
- typedef struct Line {
-
- struct Line *next; /* Pointer to next item */
- char **dpart[3];
- char **DateLine[3]; /* contains the date in form yy-mm-dd */
- char *RestLine; /* list of the Values of the line */
- } Line;
-
- In my programm I have to load a ascii file, my approch was to build a linked list
- of struct line (which I defiened with typedef Line), a struct line will contain data
- and a pointer to the next line. this part went fine but the problems starts with
- clearing a file from memory, my approch to that was as follows. The errors created
- by this function occor in two different ways, 1) they damage a line which was loaded
- after the clearing 2) causes the programm to crash with a segmentation fault.
- I know I have made a mistake in my memeroy administration but, I can't find the problem
-
- void ClearLines()
- {
- Line* line;
- Line* line_next;
-
-
- line=FirstLine;
-
- /*
- * when no file is loaded the firstline will be NULL
- */
-
- while(line!=(Line*)NULL && line->next!=(Line*)NULL)
- {
- /*
- * ask for the next line while the currentline is still around
- */
-
- line_next=line->next;
-
- /*
- * delete the date in both sequences
- */
-
- free(line->dpart[0]);
- free(line->dpart[1]);
- free(line->dpart[2]);
- free(line->DateLine[0]);
- free(line->DateLine[1]);
- free(line->DateLine[2]);
-
- /*
- * delete the rest of the line
- */
-
- free(line->RestLine);
-
- /*
- * delete structures of the line
- */
-
- free(*line->dpart);
- free(*line->DateLine);
-
- /*
- * delete the line itself and let 'line' point to the next line
- */
-
- free(&line); /* I suspect this line to trigger the error */
- line=line_next;
- }
- }
-
-
- I hope some can find what I did wrong, I'm sure it is something simple but I can't
- find it....
-
- Thanx for reading my question
-
- please email me if you have a solution... (rikko@nioz.nl)
-
- Greetings
- Rikko
-
-
-